package com.zyh.homework;

import com.zyh.homework.domain.User;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zyh
 * @Date 2024/8/16 17:01
 * @Description
 */

public class Test1 {
    private static List<User> users = Arrays.asList(
            new User(1, "亚索", "巨神峰", 20, 99, new Date(), Arrays.asList("吹风", "吹笛子", "睡觉")),
            new User(2, "德玛", "德玛西亚", 21, 95, new Date(), Arrays.asList("大保健", "冲锋", "睡觉")),
            new User(3, "光辉", "德玛西亚", 22, 80, new Date(), Arrays.asList("魔法棒", "激光", "睡觉")),
            new User(4, "发条", "黑色玫瑰", 23, 75, new Date(), Arrays.asList("玩球", "上发条")),
            new User(5, "铁男", "黑色玫瑰", 24, 75, new Date(), Arrays.asList("棒子", "小黑屋", "睡觉")),
            new User(6, "提莫", "巨神峰", 25, 70, new Date(), Arrays.asList("种蘑菇", "采蘑菇", "吹笛子")),
            new User(7, "卢锡安", "巨神峰", 26, 60, new Date(), Arrays.asList("双枪", "滑步")),
            new User(8, "卡牌大师", "诺克萨斯", 27, 65, new Date(), Arrays.asList("玩牌", "黄牌")),
            new User(9, "雷克顿", "德玛西亚", 28, 60, new Date(), Arrays.asList("飞顿", "睡觉"))
    );

    //1.拿到所有用户的名字集合
    @Test
    public void test1() {
        List<String> nameList = users.stream().map(i -> i.getName()).collect(Collectors.toList());
        nameList.forEach(System.out::println);
//        System.out.println(nameList);
    }

    //2.拿到年龄小于25的人的名字集合
    @Test
    public void test2() {
        List<String> ageNameList = users.stream().filter(item -> item.getAge() < 25).map(item -> item.getName()).collect(Collectors.toList());
        ageNameList.forEach(System.out::println);
    }

    //3.根据分数倒排序
    @Test
    public void test3() {
        List<User> scoreList = users.stream().sorted((i1, i2) -> i1.getScore() > i2.getScore() ? -1 : (i1.getScore() < i2.getScore()) ? 1 : 0).collect(Collectors.toList());
        scoreList.forEach(System.out::println);
    }

    //4.拿到爱好里面有睡觉的 集合
    @Test
    public void test4() {
        List<User> sleepList = users.stream().filter(i -> i.getHobby().contains("睡觉")).collect(Collectors.toList());
        sleepList.forEach(System.out::println);
    }

    //5.拿到来自德玛西亚的用户，根据分数排序，取前两个的名字
    @Test
    public void test5() {
        List<User> scoreList = users.stream().filter(i -> i.getAddr().contains("德玛西亚")).sorted((i1, i2) -> i1.getScore() > i2.getScore() ? 1 : (i1.getScore() == i2.getScore() ? 0 : -1)).collect(Collectors.toList());
        for (int i = 0; i < 2; i++) {
            System.out.println(scoreList.get(i).getName());
        }
    }

    //6.计算分数总和
    @Test
    public void test6() {
        Integer sumScore = users.stream().collect(Collectors.summingInt(User::getScore));
        System.out.println("总分" + sumScore);
    }
}
